//
//  CNSearchViewCell.m
//  BeeQuick_19
//
//  Created by whong7 on 16/9/23.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNSearchViewCell.h"


@interface CNSearchViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *specificsLabel;
@property (weak, nonatomic) IBOutlet UILabel *partner_priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *market_priceLabel;
@property (weak, nonatomic) IBOutlet UIImageView *pm_descView;


@property(nonatomic,assign)NSInteger  selfCount;

@property (weak, nonatomic) IBOutlet UILabel *countLabel;
@property (weak, nonatomic) IBOutlet UIButton *addButton;
@property (weak, nonatomic) IBOutlet UIButton *reduceButton;



@end

@implementation CNSearchViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

-(void)setModel:(CNSearchModel *)model
{
    _model = model;
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:model.img]];
    self.nameLabel.text = model.name;
    self.specificsLabel.text = model.specifics;
    self.partner_priceLabel.text = model.price;
    self.market_priceLabel.text = model.market_price;
    self.selfCount = model.count;
    [self setupCount];
    
    if (model.pm_desc) {
        self.pm_descView.alpha = 1;
    }
    else
    {
        self.pm_descView.alpha = 0;
    }
    
}


- (IBAction)addClick:(id)sender {
    self.selfCount++;
    [self setupCount];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"点击加号" object:nil userInfo:@{@"selectCell":self,@"selecedImageView":self.iconView}];
    //修改数据源数据，防止cell复用
    self.cellBlock(self.indexPath,self.selfCount);
    

    //对应数据+1
    [[SQLiteManager sharedSQLiteManager] charu:self.model With: +1];
    
    
    
}
- (IBAction)reduceClick:(id)sender {
    self.selfCount--;
    [self setupCount];
    
    self.cellBlock(self.indexPath,self.selfCount);
    
    //对应数据—1
    [[SQLiteManager sharedSQLiteManager] charu:self.model With:-1];
}

-(void)setupCount
{
    if (self.selfCount != 0) {
        self.reduceButton.alpha = 1;
        self.countLabel.alpha = 1;
        self.countLabel.text = [NSString stringWithFormat:@"%ld",(long)self.selfCount];
        
    }
    else
    {
        self.reduceButton.alpha = 0;
        self.countLabel.alpha = 0;
    }

}




@end
